package com.op.teachingliteapp.fragment.resource;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.op.teachingliteapp.R;



public class ResourceGridAdapter extends BaseAdapter{
	private ArrayList<ResourceCellDataProvider>  list;
	public  int size;
	 private Context mContext;
	 private TextView name_tv;
	 private ImageView img;
	 public StringBuffer checkBox_chosen=new StringBuffer();
	 public CheckBox deleteView;
	 private boolean isShowDelete;//根据这个变量来判断是否显示删除图标，true是显示，false是不显示

	 public ResourceGridAdapter(Context mContext,ArrayList<ResourceCellDataProvider> list) {
	  this.mContext = mContext;
	  this.list=list;
	  list.add(new ResourceCellDataProvider("课程PPT",R.drawable.ppt1));
	  list.add(new ResourceCellDataProvider("上机实验",R.drawable.sjsy1));
	  list.add(new ResourceCellDataProvider("视频资料",R.drawable.spzl1));
	  list.add(new ResourceCellDataProvider("课程案例",R.drawable.kcal1));
	  list.add(new ResourceCellDataProvider("上课计划",R.drawable.kcjh1));
	  list.add(new ResourceCellDataProvider("学生名单",R.drawable.xsmd1));
	  list.add(new ResourceCellDataProvider("添加文件夹", R.drawable.add_course_normal));
	 }
	 public void setIsShowDelete(boolean isShowDelete){
	  this.isShowDelete=isShowDelete;
	  notifyDataSetChanged();
	 }
	 
		public void add(ResourceCellDataProvider lcd){
            list.add(list.size()-1,lcd);
            notifyDataSetChanged();
			}
	 
		public Context getContext(){
			return mContext;
		}
	

	 public 	void remove(int position) {
		 list.remove(position);		
	     notifyDataSetChanged();
		}
	 
	 @Override
	 public int getCount() {
		 return list.size();
	 }

	 @Override
	 public ResourceCellDataProvider getItem(int position) {
		 return list.get(position);
	 }

	 @Override
	 public long getItemId(int position) {
	  return position;
	 }

	 @SuppressLint({ "ViewHolder", "InflateParams" })
	@Override
	 public View getView(final int position, View convertView, ViewGroup parent) {

		LinearLayout gl=null;
		if(convertView!=null){
			gl=(LinearLayout)convertView;
			}else{
				gl=(LinearLayout)LayoutInflater.from(getContext()).inflate(R.layout.item_course,null);
			}
		final ResourceCellDataProvider data=getItem(position);
		
		
	  img = (ImageView)gl.findViewById(R.id.class_img);
	  name_tv = (TextView) gl.findViewById(R.id.class_name);
	  
	  img.setImageResource(data.iconId);
	  name_tv.setText(data.name);
	  
	  deleteView = (CheckBox)gl.findViewById(R.id.class_checkbox);
	  if(position<list.size()-1){
		  deleteView.setVisibility(isShowDelete?View.VISIBLE:View.GONE);//设置删除按钮是否显示
		  notifyDataSetChanged();
	  }
	  
	  deleteView.setOnCheckedChangeListener(new OnCheckedChangeListener() {
		  
		  @Override
		  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
			  if(isChecked){
				  Toast.makeText(getContext(),"选中"+position,Toast.LENGTH_SHORT).show();
				  checkBox_chosen.append(data.name);//记录选择课程
				  
			  }else{
				  Toast.makeText(getContext(),"没有选中"+position,Toast.LENGTH_SHORT).show();
				  size=checkBox_chosen.indexOf(data.name);
				  checkBox_chosen.delete(size,size+data.name.length());//删除已选课程
			  }
			  
		  }
	  });
	  
	  return gl;
	 }

}
